package vip.xiaonuo.hontu.issued.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.iservice.IMemberDeviceService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.mapper.MemberDeviceMapper;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.mapper.MemberUserMapper;
import vip.xiaonuo.facedoor.rule.HQRulesEngine;
import vip.xiaonuo.facedoor.rule.HTRulesEngine;
import vip.xiaonuo.facedoor.rule.RuleConstant;

@Service(value =RuleConstant.HT + RuleConstant.MEMBER_DEVICESE)
public class HTMemberDeviceServiceImpl implements IMemberDeviceService {

    @Resource
    private MemberUserMapper memberUserMapper;

    @Resource
    private MemberDeviceMapper memberDeviceMapper;

    @Resource
    private DeviceBaseService deviceBaseService;

    private void baseCheck(MemberDevice memberDevice) {
        if (memberDevice == null) {
            throw new ServiceException(500, "找不到关联信息！");
        }
        if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD) || memberDevice.getStatus().equals(MemberDeviceConstant.UPDATE) || memberDevice.getStatus().equals(MemberDeviceConstant.DELETE)) {
            throw new ServiceException(500, "非正常状态 不允许操作！");
        }
    }
    @Override
    public void delete(Long id) throws Exception {
        MemberDevice memberDevice = memberDeviceMapper.selectById(id);
        baseCheck(memberDevice);
        DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
        MemberUser memberUser = memberUserMapper.selectById(memberDevice.getMemberId());
        // 新增失败 直接删除人员
        if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
            memberDeviceMapper.deleteById(id);
        } else {
            HTRulesEngine.memberDealWithService.delete(memberDevice, memberUser, null, deviceBase);
        }
    }

    @Override
    public void sync(Long id) throws Exception {
        MemberDevice memberDevice = memberDeviceMapper.selectById(id);
        baseCheck(memberDevice);
        if (memberDevice.getStatus().equals(MemberDeviceConstant.SYNC_SUCCESS)) {
            throw new ServiceException(500, "未发生错误 不允许同步！");
        }
        DeviceBase deviceBase = deviceBaseService.getById(memberDevice.getDeviceId());
        MemberUser memberUser = memberUserMapper.selectById(memberDevice.getMemberId());
        // 恢复人员状态
        if (memberDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
            HTRulesEngine.memberDealWithService.add(memberDevice, memberUser, null, deviceBase);
        }
        if (memberDevice.getStatus().equals(MemberDeviceConstant.UPDATE_FAIL)) {
            HTRulesEngine.memberDealWithService.update(memberDevice, memberUser, null, deviceBase);
        }
        if (memberDevice.getStatus().equals(MemberDeviceConstant.DELETE_FAIL)) {
            HTRulesEngine.memberDealWithService.delete(memberDevice, memberUser, null, deviceBase);
        }
        memberDeviceMapper.updateById(memberDevice);
    }
}
